﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FastReflectionLib;
using System.Web;
using SZHL.UI.AppCode;
using SZHL.Data;
using Newtonsoft.Json;
using System.Data;

namespace SZHL.UI.API
{
    public class LCSPManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(LCSPManage).GetMethod(msg.Action.ToUpper());
            LCSPManage model = new LCSPManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 流程审批添加
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDLCSP(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_LCSP lcsp = JsonConvert.DeserializeObject<ZH_LCSP>(P1);
            if (lcsp.Content.Trim() == "")
            {
                msg.ErrorMsg = "内容不能为空";
                return;
            }
            if (lcsp.LeiBie.Trim() == "")
            {
                msg.ErrorMsg = "请选择类别";
                return;
            }
            if (P2 != "") // 处理微信上传的图片
            {
                string fids = CommonHelp.ProcessWxIMG(P2, UserInfo.User.UserName);
                if (!string.IsNullOrEmpty(lcsp.Files))
                {
                    lcsp.Files += "," + fids;
                }
                else
                {
                    lcsp.Files = fids;
                }
            }

            if (lcsp.ID == 0)
            {
                lcsp.CRDate = DateTime.Now;
                lcsp.CRUser = UserInfo.User.UserName;
                lcsp.BranchName = new JH_Auth_BranchB().GetBranchNameByCode(UserInfo.User.BranchCode);
                lcsp.ShenQingRen = string.IsNullOrEmpty(lcsp.ShenQingRen) ? UserInfo.User.UserRealName : lcsp.ShenQingRen;
                lcsp.BranchNo = UserInfo.User.BranchCode;
                new ZH_LCSPB().Insert(lcsp);
                SendStartMsg(UserInfo, lcsp.intProcessStanceid);
            }
            else
            {
                new ZH_LCSPB().Update(lcsp);
            }
            msg.Result = lcsp;
        }
        /// <summary>
        /// 我填写的流程审批//失效
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">类别</param>
        /// <param name="P2">审批内容</param>
        /// <param name="UserInfo"></param>
        public void GETMYLCSPLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = "Where  1=1";
            string branch = context.Request["b"] ?? "";
            if (branch != "")
            {
                string strQueryUser = context.Request["QUSER"] ?? "";
                if (strQueryUser != "")
                {
                    strWhere += string.Format(" And  lc.CRUser in('{0}')", strQueryUser.ToFormatLike());
                }
                else
                {
                    strWhere += string.Format("  And lc.BranchNo = '{0}'", UserInfo.User.BranchCode);
                }

            }
            else
            {
                strWhere += string.Format(" And lc.CRUser='{0}' ", UserInfo.User.UserName);
            }
            if (P1 != "")
            {
                strWhere += string.Format(" And LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And   Content like '%{0}%'", P2);
            }
            string strSQL = string.Format(@"SELECT lc.*, case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                            when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName
                                            from ZH_LCSP lc inner join  Yan_WF_PI wfpi  on lc.intProcessStanceid=wfpi.ID   " + strWhere + " order by lc.CRDate desc");
            DataTable dt = new Yan_WF_TDB().GetDTByCommand(strSQL);
            msg.Result = dt;
        }
        /// <summary>
        /// 我填写的流程审批
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">类别</param>
        /// <param name="P2">审批内容</param>
        /// <param name="UserInfo"></param>
        public void GETMYLCSPLIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = "1=1 ";
            string branch = context.Request["b"] ?? "";
            if (branch != "")
            {
                string strQueryUser = context.Request["QUSER"] ?? "";
                if (strQueryUser == "")
                {
                    strQueryUser = new JH_Auth_BranchB().GetAllUserByBranch(branch);
                }
                strWhere += string.Format(" And  lc.CRUser in('{0}')", strQueryUser.ToFormatLike());

            }
            else
            {
                strWhere += string.Format(" And lc.CRUser='{0}' ", UserInfo.User.UserName);
            }
            if (P1 != "")
            {
                strWhere += string.Format(" And LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And   Content like '%{0}%'", P2);
            }
            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "1", out page);//页码
            page = page == 0 ? 1 : page;
            int totalCount = 0;
            DataTable dt = new Yan_WF_TDB().GetDataPager("ZH_LCSP lc inner join  Yan_WF_PI wfpi  on lc.intProcessStanceid=wfpi.ID ", @" lc.*, case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                            when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName", 8, page, "lc.CRDate desc", strWhere, ref totalCount);

            msg.Result = dt;
            msg.Result1 = totalCount;
        }
        /// <summary>
        /// 获取审批列表//后台管理
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETLCSPLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " where 1=1  ";

            if (P1 != "")
            {
                strWhere += string.Format(" And lc. LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And   lc.Content like '%{0}%'", P2);
            }
            string strSQL = string.Format(@"SELECT lc.*, case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                            when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName
                                            from ZH_LCSP lc inner join  Yan_WF_PI wfpi  on lc.intProcessStanceid=wfpi.ID   " + strWhere + " order by lc.CRDate desc");
            DataTable dt = new Yan_WF_TDB().GetDTByCommand(strSQL);

            msg.Result = dt;
        }
        /// <summary>
        /// 获取审批列表//微信
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETLCSPLIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " 1=1  ";

            if (P1 != "")
            {
                strWhere += string.Format(" And lc. LeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And   lc.Content like '%{0}%'", P2);
            }
            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "1", out page);//页码
            page = page == 0 ? 1 : page;
            int totalCount = 0;
            DataTable dt = new Yan_WF_TDB().GetDataPager("ZH_LCSP lc inner join  Yan_WF_PI wfpi  on lc.intProcessStanceid=wfpi.ID ", @" lc.*, case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                            when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName", 8, page, "lc.CRDate desc", strWhere, ref totalCount);

            msg.Result = dt;
            msg.Result1 = totalCount;
        }


        public void GETLCSPMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            if (!string.IsNullOrEmpty(P1))
            {
                ZH_LCSP lcsp = new ZH_LCSPB().GetEntity(d => d.ID.ToString() == P1);
                msg.Result = lcsp;//返回表单数据
                msg.Result1 = new Yan_WF_PIB().GETPDMODELBYID(lcsp.intProcessStanceid);//返回初始化数据 
                if (!string.IsNullOrEmpty(lcsp.Files))
                {
                    string[] fileIds = lcsp.Files.ToString().Split(',');
                    msg.Result2 = new FT_FileB().GetEntities(p => fileIds.Contains(p.ID.ToString()));
                }
            }
            else
            {
                msg.Result1 = new Yan_WF_PDB().GetEntity(d => d.ID.ToString() == P2);//返回初始化数据 
            }
        }
        /// <summary>
        /// 删除流程审批
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELLCSPBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {

                if (new ZH_LCSPB().Delete(d => d.ID.ToString() == P1))
                {
                    msg.ErrorMsg = "";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 流程审批数量//失效
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETLCSPMENUCOUNT(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = "select Count(0) from ZH_LCSP";
            string strWhere = string.Format(" where  BranchNo= '{0}'", UserInfo.User.BranchCode);
            msg.Result = new ZH_GZRZB().ExsSclarSql(strSql + strWhere);
            strWhere = string.Format(" where CRUser= '" + UserInfo.User.UserName + "'");
            msg.Result1 = new ZH_GZRZB().ExsSclarSql(strSql + strWhere);
        }
        #region 流程管理
        /// <summary>
        /// 获取流程列表 P1==""流程设置列表，P1!="" 自定义流程添加选择列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETWFPDLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            if (P1 != "")
            {
                msg.Result = new Yan_WF_PDB().GetEntities(d => d.lcstatus.ToString() == P1 && d.IsSuspended == "Y");
            }
            else
            {
                string strSql = string.Format(@"SELECT DISTINCT wfpd.ProcessName,wfpd.ID,count(wfpi.ID) formCount,wfpd.lcstatus,wfpd.IsSuspended from Yan_WF_PD wfpd LEFT join Yan_WF_PI wfpi on wfpd.ID=wfpi.PDID group by wfpd.ProcessName,wfpd.ID,wfpd.lcstatus,wfpd.IsSuspended");
                msg.Result = new Yan_WF_PDB().GetDTByCommand(strSql);
            }
        }
        /// <summary>
        /// 流程审批添加
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDPROCESS(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            Yan_WF_PD lcsp = JsonConvert.DeserializeObject<Yan_WF_PD>(P1);
            if (lcsp.ProcessName.Trim() == "")
            {
                msg.ErrorMsg = "流程名称不能为空";
                return;
            }

            if (lcsp.ID == 0)
            {
                lcsp.lcstatus = 1;
                if (new Yan_WF_PDB().GetEntities(d => d.ProcessName == lcsp.ProcessName).Count() > 0)
                {

                    msg.ErrorMsg = "已存在此流程";
                    return;
                }
                lcsp.Tempcontent = @"<div class='form-group data-control' datatype='textarea' dataname='表单内容'>
                                    <label class='col-sm-2 control-label'>表单内容</label>
                                    <div class='col-sm-9'>
                                        <textarea class='form-control szhl_UEEDIT'></textarea>
                                    </div>
                                </div>";
                new Yan_WF_PDB().Insert(lcsp);
            }
            else
            {
                new Yan_WF_PDB().Update(lcsp);
            }
            msg.Result = lcsp;
        }
        /// <summary>
        /// 获取流程信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETPROCESSBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new Yan_WF_PDB().GetEntity(d => d.ID.ToString() == P1);
        }
        /// <summary>
        /// 删除流程信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELPROCESSBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {

                if (new Yan_WF_PDB().Delete(d => d.ID.ToString() == P1))
                {
                    msg.ErrorMsg = "";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }








        /// <summary>
        /// 开始流程
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">启动流程的应用Code</param>
        /// <param name="P2">审核人信息</param>
        /// <param name="UserInfo"></param>
        public void STARTWF(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                int PDID = 0;
                int.TryParse(context.Request["PDID"] ?? "0", out PDID);
                if (PDID == 0)
                {
                    //如果找不到PDID,就通过FORMCODE找PDID
                    string strFormCode = P1;
                    JH_Auth_FormBase MODEL = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == P1);
                    PDID = MODEL.FormProcessDefinitionID.Value;
                }
                Yan_WF_PD PD = new Yan_WF_PDB().GetEntity(d => d.ID == PDID);
                string strTZR = P2;
                msg.Result = new Yan_WF_PIB().StartWF(PD, UserInfo.User.UserName, P2);
                //发送消息
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }



        /// <summary>
        /// 获取流程的具体步骤
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETTDLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new Yan_WF_TDB().GetEntities(d => d.ProcessDefinitionID.ToString() == P1);
        }
        /// <summary>
        /// 添加流程模版信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDTDINFO(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                List<Yan_WF_TD> tdList = JsonConvert.DeserializeObject<List<Yan_WF_TD>>(P1);
                if (new Yan_WF_TDB().Insert(tdList))
                {
                    new Yan_WF_TDB().Delete(d => d.ProcessDefinitionID == tdList[0].ProcessDefinitionID && d.ID < tdList[0].ID);
                }
                string strSql = string.Format("update Yan_WF_PD set ProcessType=1 where ID={0}", tdList[0].ProcessDefinitionID);
                new Yan_WF_PDB().ExsSql(strSql);
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = "出现异常，稍后重试";
            }

        }


        public void MANAGEWF(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                string strShUser = context.Request["SHUser"] ?? "";
                int PID = int.Parse(P1);
                Yan_WF_PIB PIB = new Yan_WF_PIB();
                if (PIB.isCanSP(UserInfo.User.UserName, PID) == "Y")//先判断用户能不能处理此流程
                {
                    List<string> ListNextUser = new List<string>();

                    new Yan_WF_PIB().MANAGEWF(UserInfo.User.UserName, PID, P2, ref ListNextUser, strShUser);//处理任务
                    if (ListNextUser.Count() > 0)//找到人了
                    {
                        string formcode = context.Request["formcode"] ?? "";
                        int ID = 0;
                        int.TryParse(context.Request["ID"] ?? "0", out ID);
                        if (ID > 0 && !string.IsNullOrEmpty(formcode))
                        {
                            Yan_WF_PI PI = new Yan_WF_PIB().GetEntity(d => d.ID == PID);
                            JH_Auth_UserB.UserInfo CRUserInfo = new JH_Auth_UserB.UserInfo(PI.CRUser);//获取创建者的用户信息
                            string content = CRUserInfo.User.UserRealName + "发起了" + new Yan_WF_PDB().GetEntity(d => d.ID == PI.PDID.Value).ProcessName + "表单";
                            new JH_Auth_User_CenterB().SendMsg(CRUserInfo, formcode.ToLower(), "流程提醒", content, ID, ListNextUser.ListTOString(','), PID);
                        }
                    }
                    else//找不到人了,结束该流程
                    {
                        new Yan_WF_PIB().ENDWF(PID);//结束流程
                        string formcode = context.Request["formcode"] ?? "";
                        int ID = 0;
                        int.TryParse(context.Request["ID"] ?? "0", out ID);
                        if (ID > 0 && !string.IsNullOrEmpty(formcode))
                        {
                            Yan_WF_PI PI = new Yan_WF_PIB().GetEntity(d => d.ID == PID);
                            string content = UserInfo.User.UserRealName + "审批完成了" + new Yan_WF_PDB().GetEntity(d => d.ID == PI.PDID.Value).ProcessName + "表单";
                            new JH_Auth_User_CenterB().SendMsg(UserInfo, formcode.ToLower(), "流程提醒", content, ID, PI.CRUser, PID);
                        }
                    }
                }
                else
                {
                    msg.ErrorMsg = "该流程已被处理,您已无法处理此流程";
                }




            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }




        /// <summary>
        /// 退回当前流程
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void REBACKWF(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                int PID = int.Parse(P1);
                Yan_WF_PIB PIB = new Yan_WF_PIB();
                if (PIB.isCanSP(UserInfo.User.UserName, PID) == "Y")//先判断用户能不能处理此流程
                {
                    new Yan_WF_PIB().REBACKLC(UserInfo.User.UserName, PID, P2);//结束任务
                    string formcode = context.Request["formcode"] ?? "";
                    int ID = 0;
                    int.TryParse(context.Request["ID"] ?? "0", out ID);
                    if (ID > 0 && !string.IsNullOrEmpty(formcode))
                    {
                        Yan_WF_PI PI = new Yan_WF_PIB().GetEntity(d => d.ID == PID);
                        string content = UserInfo.User.UserRealName + "退回了" + new Yan_WF_PDB().GetEntity(d => d.ID == PI.PDID.Value).ProcessName + "表单";
                        new JH_Auth_User_CenterB().SendMsg(UserInfo, formcode.ToLower(), "流程提醒", content, ID, PI.CRUser, PID);
                    }
                }
                else
                {
                    msg.ErrorMsg = "该流程已被处理,您已无法处理此流程";
                }





            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }


        /// <summary>
        /// 获取流程数据(返回流程数据以及该流程的任务数据)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETWFDATA(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                Yan_WF_PI PIMODEL = new Yan_WF_PIB().GetEntity(d => d.ID == int.Parse(P1));
                Yan_WF_PD PD = new Yan_WF_PDB().GetEntity(d => d.ID == PIMODEL.PDID);
                DataTable dtList = new DataTable();
                DataTable dt = new DataTable();
                //固定流程获取流程处理数据
                if (PD.ProcessType == "0") //0自由流程,1固定流程
                { //获取流程处理数据
                    dtList = new Yan_WF_TIB().GetEntities(d => d.PIID == int.Parse(P1)).ToDataTable();
                    dtList.Columns.Add("userrealname");
                    dtList.Columns.Add("state");
                    dtList.Columns.Add("status");
                    for (int i = 0; i < dtList.Rows.Count; i++)
                    {
                        dtList.Rows[i]["userrealname"] = new JH_Auth_UserB().GetUserRealName(dtList.Rows[i]["TaskUserID"].ToString());
                        dtList.Rows[i]["state"] = dtList.Rows[i]["TaskState"];
                    }
                }
                else
                {
                    dtList = new Yan_WF_TDB().GetEntities(d => d.ProcessDefinitionID == PIMODEL.PDID.Value).ToDataTable();
                    dtList.Columns.Add("userrealname");
                    dtList.Columns.Add("EndTime");
                    dtList.Columns.Add("TaskUserView");
                    dtList.Columns.Add("state");
                    //var bl = true;

                    //if (PIMODEL.IsCanceled == "Y")
                    //{
                    //    bl = false;
                    //}

                    //dt = dtList.Copy();
                    //for (int i = 0; i < dtList.Rows.Count; i++)
                    //{
                    //    Yan_WF_TI tiModel = new Yan_WF_TIB().GetEntity(d => d.PIID.ToString() == P1 && d.TDCODE == dt.Rows[i]["TDCODE"].ToString() && d.EndTime != null);
                    //    if (tiModel != null)
                    //    {
                    //        dt.Rows[i]["userrealname"] = new JH_Auth_UserB().GetUserRealName(tiModel.TaskUserID);
                    //        dt.Rows[i]["EndTime"] = tiModel.EndTime;
                    //        dt.Rows[i]["TaskUserView"] = tiModel.TaskUserView;
                    //    }
                    //    else
                    //    {
                    //        if (bl)
                    //        {
                    //            bl = false;
                    //            dt.Rows[i]["EndTime"] = "";
                    //            dt.Rows[i]["TaskUserView"] = "";
                    //            dt.Rows[i]["userrealname"] = "";
                    //        }
                    //        else
                    //        {
                    //            dt.Rows.Remove(dt.Rows[i]);
                    //        }
                    //    }
                    //} 
                    foreach (DataRow dr in dtList.Rows)
                    {
                        Yan_WF_TI tiModel = new Yan_WF_TIB().GetEntity(d => d.PIID.ToString() == P1 && d.TDCODE == dr["TDCODE"].ToString() && d.EndTime != null);
                        if (tiModel != null)
                        {
                            dr["userrealname"] = new JH_Auth_UserB().GetUserRealName(tiModel.TaskUserID);
                            dr["EndTime"] = tiModel.EndTime;
                            dr["TaskUserView"] = tiModel.TaskUserView;
                            dr["state"] = tiModel.TaskState;
                        }
                        else
                        {
                            dr["EndTime"] = "";
                            dr["TaskUserView"] = "";
                            dr["userrealname"] = "";
                            dr["state"] = "";
                            if (PIMODEL.IsCanceled != "Y")
                            { dr["state"] = "0"; }
                        }
                    }
                }

                //获取流程处理数据
                msg.Result = PIMODEL;
                msg.Result1 = dtList;
                msg.Result2 = new Yan_WF_PIB().isCanSP(UserInfo.User.UserName, int.Parse(P1));
                msg.Result3 = PD.ProcessType;


            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取待审批列表（旧版我的审批使用）
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETDSHDATA(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            List<string> intPro = new Yan_WF_PIB().GetDSH(UserInfo.User.UserName).Select(d => d.PIID.ToString()).ToList();
            if (intPro.Count > 0)
            {
                string strSQL = string.Format(@" SELECT '出差请假' as  Name ,'CCXJ' ProName,intProcessStanceid,ID,CRUser,CRDate,ShenQingRen,'正在审批' StateName from  ZH_CCXJ  WHERE intProcessStanceid in ({0}) 
                                            UNION ALL
                                            SELECT '经费报销' as Name,'JFBX' ProName,intProcessStanceid,ID,CRUser,CRDate,ShenQingRen,'正在审批' StateName from ZH_JFBX  WHERE intProcessStanceid in ({0}) 
                                             UNION ALL
                                            SELECT LeiBie+'流程审批' as Name,'LCSP' ProName,intProcessStanceid,ID,CRUser,CRDate,ShenQingRen,'正在审批' StateName from ZH_LCSP  WHERE intProcessStanceid in ({0})", intPro.ListTOString(','));
                msg.Result = new Yan_WF_PIB().GetDTByCommand(strSQL).OrderBy(" CRDate desc");
            }
        }
        /// <summary>
        /// 获取待审批列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETDSHDATALIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            List<string> intPro = new Yan_WF_PIB().GetDSH(UserInfo.User.UserName).Select(d => d.PIID.ToString()).ToList();
            if (intPro.Count > 0)
            {
                string tableName = string.Format(@" (SELECT '出差请假' as  Name ,'CCXJ' ProName,intProcessStanceid,ID,CRUser,CRDate,ShenQingRen,'正在审批' StateName,0 lcType from  ZH_CCXJ  WHERE intProcessStanceid in ({0}) 
                                            UNION ALL
                                            SELECT '经费报销' as Name,'JFBX' ProName,intProcessStanceid,ID,CRUser,CRDate,ShenQingRen,'正在审批' StateName,0 lcType from ZH_JFBX  WHERE intProcessStanceid in ({0}) 
                                             UNION ALL
                                            SELECT LeiBie+'流程审批' as Name,'LCSP' ProName,intProcessStanceid,ZH_LCSP.ID,ZH_LCSP.CRUser,ZH_LCSP.CRDate,ZH_LCSP.ShenQingRen,'正在审批' StateName,pd.ProcessType lcType  from ZH_LCSP 
                                            inner join  Yan_WF_PI  wf_pi on intProcessStanceid=wf_pi.ID inner join Yan_WF_PD pd on wf_pi.PDID=pd.ID
                                            WHERE intProcessStanceid in ({0}))as NewTable", intPro.ListTOString(','));
                int total = 0;
                int page = 0;
                int.TryParse(P2, out page);
                page = page == 0 ? 1 : page;
                DataTable dt = new ZH_XTGGB().GetDataPager(tableName, "*", 8, page, " CRDate desc", "", ref total);
                msg.Result = dt;
                msg.Result1 = total;
            }
        }
        /// <summary>
        /// 获取待审核已审核数量（旧版我的审批获取数量）
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETWFMENUCOUNT(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSQL = string.Format("SELECT ti.EndTime from Yan_WF_PI pi inner join  Yan_WF_TI  ti on pi.ID=ti.PIID where ti.TaskUserID='{0}' AND  (TaskUserView!='发起表单' or  TaskUserView is null) ", UserInfo.User.UserName);
            DataTable dt = new Yan_WF_PIB().GetDTByCommand(strSQL);
            msg.Result = dt.FilterTable("EndTime is null").Rows.Count;
            msg.Result1 = dt.FilterTable("EndTime is not null").Rows.Count;
        }
        /// <summary>
        /// 已审核列表(旧版我的审批使用)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETYSPLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            List<Yan_WF_TI> ListData = new Yan_WF_TIB().GetEntities("TaskUserID ='" + UserInfo.User.UserName + "' AND EndTime IS NOT NULL AND TaskUserView!='发起表单'").ToList();
            List<string> intPro = ListData.Select(d => d.PIID.ToString()).ToList();
            if (intPro.Count > 0)
            {
                string strSQL = string.Format(@"SELECT '出差请假' as  Name ,'CCXJ' ProName,intProcessStanceid,cc.ID,cc.CRUser,cc.CRDate,ShenQingRen , case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                                  when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName  from  ZH_CCXJ  cc inner join  Yan_WF_PI wfpi  on cc.intProcessStanceid=wfpi.ID   WHERE intProcessStanceid in ({0})
                                            UNION ALL
                                            SELECT '经费报销' as Name,'JFBX' ProName,intProcessStanceid,jf.ID,jf.CRUser,jf.CRDate,ShenQingRen,case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                                  when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName from ZH_JFBX  jf inner join  Yan_WF_PI wfpi  on jf.intProcessStanceid=wfpi.ID  WHERE intProcessStanceid in ({0})
                                             UNION ALL
                                            SELECT LeiBie+'流程审批' as Name,'LCSP' ProName,intProcessStanceid,lc.ID,lc.CRUser,lc.CRDate,ShenQingRen,case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                                  when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName from ZH_LCSP lc inner join  Yan_WF_PI wfpi  on lc.intProcessStanceid=wfpi.ID   WHERE intProcessStanceid in ({0})", intPro.ListTOString(','));
                msg.Result = new Yan_WF_PIB().GetDTByCommand(strSQL).OrderBy(" CRDate desc");
            }
        }
        /// <summary>
        /// 已审核列表(旧版我的审批使用)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETYSPLIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            List<Yan_WF_TI> ListData = new Yan_WF_TIB().GetEntities("TaskUserID ='" + UserInfo.User.UserName + "' AND EndTime IS NOT NULL AND TaskUserView!='发起表单'").ToList();
            List<string> intPro = ListData.Select(d => d.PIID.ToString()).ToList();
            if (intPro.Count > 0)
            {
                string tableName = string.Format(@"(SELECT '出差请假' as  Name ,'CCXJ' ProName,intProcessStanceid,cc.ID,cc.CRUser,cc.CRDate,ShenQingRen , case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                                  when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName  from  ZH_CCXJ  cc inner join  Yan_WF_PI wfpi  on cc.intProcessStanceid=wfpi.ID   WHERE intProcessStanceid in ({0})
                                            UNION ALL
                                            SELECT '经费报销' as Name,'JFBX' ProName,intProcessStanceid,jf.ID,jf.CRUser,jf.CRDate,ShenQingRen,case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                                  when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName from ZH_JFBX  jf inner join  Yan_WF_PI wfpi  on jf.intProcessStanceid=wfpi.ID  WHERE intProcessStanceid in ({0})
                                             UNION ALL
                                            SELECT LeiBie+'流程审批' as Name,'LCSP' ProName,intProcessStanceid,lc.ID,lc.CRUser,lc.CRDate,ShenQingRen,case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                                  when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName from ZH_LCSP lc inner join  Yan_WF_PI wfpi  on lc.intProcessStanceid=wfpi.ID   WHERE intProcessStanceid in ({0})) as NewTable", intPro.ListTOString(','));
                int total = 0;
                int page = 0;
                int.TryParse(P2, out page);
                page = page == 0 ? 1 : page;
                DataTable dt = new ZH_XTGGB().GetDataPager(tableName, "*", 8, page, " CRDate desc", "", ref total);
                msg.Result = dt;
                msg.Result1 = total;
            }
        }
        /// <summary>
        /// 获取待审批分页列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETDSHDATA_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            List<string> intPro = new Yan_WF_PIB().GetDSH(UserInfo.User.UserName).Select(d => d.PIID.ToString()).ToList();
            if (intPro.Count > 0)
            {
                if (P1 != "")
                {
                    int page = 0;
                    int.TryParse(P2, out page);
                    page = page == 0 ? 1 : page;
                    int total = 0;
                    DataTable dt = new ZH_XTGGB().GetDataPager("ZH_" + P1, "*", 8, page, " CRDate desc", " intProcessStanceid in (" + intPro.ListTOString(',') + ") ", ref total);
                    msg.Result = dt;
                }
            }
        }
        /// <summary>
        /// 禁用或启用流程审批类别
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void MODIFYLCSTATE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                string strSQL = string.Format("Update Yan_WF_PD set IsSuspended='{0}' where Id={1}", P1, P2);
                new Yan_WF_PDB().ExsSql(strSQL);
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取审核人列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETSPUSERLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            int PDID = 0;
            int.TryParse(context.Request["PDID"] ?? "0", out PDID);
            if (PDID == 0)
            {
                int PIID = 0;
                int.TryParse(context.Request["PIID"] ?? "0", out PIID);
                PDID = new Yan_WF_PIB().GETPDID(PIID);
            }
            if (PDID == 0)
            {
                string strFormCode = P1;
                JH_Auth_FormBase MODEL = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == P1);
                PDID = MODEL != null ? MODEL.FormProcessDefinitionID.Value : 0;
            }
            if (PDID > 0)
            {
                string[] users = new Yan_WF_PDB().GetEntity(d => d.ID == PDID).Alias.Split(',');
                List<JH_Auth_User> userList = new JH_Auth_UserB().GetEntities(d => users.Contains(d.UserName)).ToList();
                msg.Result = userList;
            }
        }
        /// <summary>
        /// 删除流程信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELPIINFO(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            if (P1 != "")//PIID
            {
                new Yan_WF_PIB().Delete(d => d.ID.ToString() == P1);
                new Yan_WF_TIB().Delete(d => d.PIID.ToString() == P1);
            }
        }

        public int GetFormIDbyPID(string strFormCode, int PID)
        {
            int intFormID = 0;
            try
            {
                string strSQL = string.Format("SELECT ID FROM ZH_LCSP WHERE intProcessStanceid='{0}'", PID);
                intFormID = int.Parse(new Yan_WF_PDB().ExsSclarSql(strSQL).ToString());
            }
            catch (Exception ex)
            {

            }
            return intFormID;
        }

        public void SendStartMsg(JH_Auth_UserB.UserInfo UserInfo, int PID)
        {
            Yan_WF_PD PD = new Yan_WF_PIB().GETPDMODELBYID(PID);
            string content = UserInfo.User.UserRealName + "发起了一个" + PD.ProcessName + "流程，请您查阅审核";
            int dataid = GetFormIDbyPID("", PID);
            string strUser = new Yan_WF_PIB().GetStartMsgUser(PID, PD.ProcessType).ListTOString(',');
            new JH_Auth_User_CenterB().SendMsg(UserInfo, "lcsp", "流程审批", content, dataid, strUser.TrimEnd(','), PID);
        }
        #endregion
    }
}